package com.cius.ssm.controller;


import com.cius.ssm.entity.Product;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现HttpRequestHandler接口的处理器
 * Created by ciuscheung on 17/4/4.
 */
public class ProductController2 implements HttpRequestHandler {

    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        List<Product> lists = new ArrayList<Product>();

        Product product1 = new Product();
        product1.setId(1);
        product1.setName("手机4");

        Product product2 = new Product();
        product2.setId(2);
        product2.setName("电脑");

        lists.add(product1);
        lists.add(product2);

//        设置模型数据
        httpServletRequest.setAttribute("lists",lists);
//        设置转发的视图
        httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/product/productList.jsp")
                .forward(httpServletRequest,httpServletResponse);

/**
    使用此方法可以通过修改response,设置响应的格式,比如响应json格式比如
    httpServletResponse.setCharacterEncoding("utf-8");
    httpServletResponse.setContentType("application/json;charset=utf-8");
    httpServletResponse.getWriter().write("json串");
**/
    }
}
